package com.android.calculator.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.widget.TextView;

import com.android.calculator.R;
import com.android.calculator.factory.OperateFactory;
import com.android.calculator.operate.OperateBase;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HomeActivity extends AppCompatActivity {
    @BindView(R.id.tv_show)
    TextView tvShow;
    @BindView(R.id.tv_0)
    TextView tv0;
    @BindView(R.id.tv_1)
    TextView tv1;
    @BindView(R.id.tv_2)
    TextView tv2;
    @BindView(R.id.tv_3)
    TextView tv3;
    @BindView(R.id.tv_4)
    TextView tv4;
    @BindView(R.id.tv_5)
    TextView tv5;
    @BindView(R.id.tv_6)
    TextView tv6;
    @BindView(R.id.tv_7)
    TextView tv7;
    @BindView(R.id.tv_8)
    TextView tv8;
    @BindView(R.id.tv_9)
    TextView tv9;
    @BindView(R.id.tv_point)
    TextView tvPoint;
    @BindView(R.id.tv_add)
    TextView tvAdd;
    @BindView(R.id.tv_divide)
    TextView tvDivide;
    @BindView(R.id.tv_multipli)
    TextView tvMultipli;
    @BindView(R.id.tv_subtract)
    TextView tvSubtract;
    @BindView(R.id.tv_clear)
    TextView tvClear;
    @BindView(R.id.tv_back)
    TextView tvBack;

    private StringBuffer sbNumFirst = new StringBuffer();
    private StringBuffer sbNumSecond = new StringBuffer();
    private OperateBase operate;
    String symbol = ""; // 当前运算符

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tv_0, R.id.tv_1, R.id.tv_2, R.id.tv_3, R.id.tv_4, R.id.tv_5,
            R.id.tv_6, R.id.tv_7, R.id.tv_8, R.id.tv_9, R.id.tv_point,
            R.id.tv_back, R.id.tv_clear, R.id.tv_add, R.id.tv_subtract, R.id.tv_multipli,
            R.id.tv_divide, R.id.tv_equal})
    public void onClick(TextView view) {
        String str = view.getText().toString();
        switch (view.getId()) {
            case R.id.tv_back:
                onBack(); // 返回
                break;
            case R.id.tv_clear:
                onClear(true); // 清除
                break;
            case R.id.tv_point:
                onPointClick();
                break;
            case R.id.tv_subtract:
                onOperateSymbolClick(str);
                break;
            case R.id.tv_add:
                onOperateSymbolClick(str);
                break;
            case R.id.tv_multipli:
                onOperateSymbolClick(str);
                break;
            case R.id.tv_divide:
                onOperateSymbolClick(str);
                break;
            case R.id.tv_equal:
                onOperateClick();
                break;
            default:
                // 初始化输入项
                inputNum(str);
                break;
        }
    }

    public void onOperateClick() {
        // 运算
        if (operate != null && sbNumSecond.length() > 0) {
            operate.numbleOne = Double.parseDouble(sbNumFirst.toString());
            operate.numbleTwo = Double.parseDouble(sbNumSecond.toString());
            double result = operate.getResult();
            sbNumFirst.replace(0, sbNumFirst.length(), result + "");
            tvShow.setText(result + "");
            onClear(false);
        }
    }

    public void onOperateSymbolClick(String symbol) {
        if (sbNumFirst.length() == 0) {
            return;
        }
        if (operate != null) {
            onOperateClick();
        }
        this.symbol = symbol;
        operate = OperateFactory.buildOperateor(symbol);
        refreshShow();
    }

    public void onPointClick() {
        if (operate == null) {
            if (sbNumFirst.length() == 0) {
                // 第一次输入
                sbNumFirst.append("0.");
            } else {
                sbNumFirst.append(".");
            }

        } else {
            if (sbNumSecond.length() == 0) {
                // 第一次输入
                sbNumSecond.append("0.");
            } else {
                sbNumSecond.append(".");
            }
        }

        refreshShow();
    }

    /**
     * 点击清除按钮时
     */
    public void onClear(boolean clearShow) {
        operate = null;
        symbol = "";
        sbNumSecond.delete(0, sbNumSecond.length());
        if (clearShow) {
            tvShow.setText("");
            sbNumFirst.delete(0, sbNumFirst.length());
        }
    }

    /**
     * 点击退格按钮时
     */
    public void onBack() {
        if (TextUtils.isEmpty(tvShow.getText().toString())) {
            return;
        }
        if (!TextUtils.isEmpty(tvShow.getText().toString()) && operate == null) {
            tvShow.setText("");
            sbNumFirst.delete(0, sbNumFirst.length());
            return;
        }
        if (operate == null) {
            sbNumFirst.deleteCharAt(sbNumFirst.length() - 1);
        } else if (sbNumSecond.length() <= 0) {
            operate = null;
            symbol = "";
        } else {
            sbNumSecond.deleteCharAt(sbNumSecond.length() - 1);
        }

        refreshShow();
    }

    public void inputNum(String num) {
        if (operate == null) {
            if (sbNumFirst.length() == 1 && sbNumFirst.indexOf("0") > 0) {
                sbNumFirst.replace(0, 1, num);
            } else {
                sbNumFirst.append(num);
            }
        } else {
            if (sbNumSecond.length() == 1 && sbNumSecond.indexOf("0") > 0) {
                sbNumSecond.replace(0, 1, num);
            } else {
                sbNumSecond.append(num);
            }
        }
        refreshShow();
    }

    public void refreshShow() {
        tvShow.setText(sbNumFirst.toString() + symbol + sbNumSecond.toString());
    }
}
